A stack is a region of memory in which programs store data such as procedure and function call return addresses, passed parameters, local variables and other related status data. Various entities can maintain one or more stacks, including for instance, the processor, programs and the operating system. Some stacks are implemented as a region in virtual memory. A pointer, referred to as the “stack pointer” is used to reference virtual memory locations in the stack when reading and writing data to physical memory locations indicated by the virtual memory locations. The reference pointer is also used when reading and writing data directly to the stack.
An allocate-on-demand stack typically consists of two regions: an allocated region and a guard region. The guard region is not mapped to physical memory. The guard region is a reserved portion of non-committed virtual memory that bounds the outermost limit of the currently allocated stack memory. In other words, it is a range of virtual addresses into which the stack pointer can be advanced, but for which actual physical memory has not yet been committed.
When a program attempts to access a guard region a memory fault is generated, referred to as a “stack overflow exception.” The exception is typically handled by some type of exception handling block. Most executing programs will not fail at this point and the overflow exception is handled transparently to a user. However, if the system cannot commit memory for a now-longer stack, the “stack overflow exception” is considered unhandled which causes a catastrophic program failure.
The stack can also be extended programmatically through a high performance memory allocator function called “allocate-a” indicated in source code as “alloca( ).” Unfortunately, with fixed sized stacks and robust program threads, the allocate-a function can cause an stack overflow exception due to exhaustion of the stack or exhaustion of memory or page file. Accordingly, because of the limitations noted above, the allocate-a function is unable to prevent stack overflow exceptions from occurring when allocating data to a stack beyond the available depth of the stack.